<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" @keyup.enter.native="submitForm">
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="物料名称"
              :labelCol="{ xs: { span: 24 }, sm: { span: 4 } }"
              :wrapperCol="{ xs: { span: 24 }, sm: { span: 20 } }"
              prop="materialName"
            >
              <a-input v-model="model.materialName" placeholder="请输入物料名称" style="width: 100%"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" placeholder="请输入规格型号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialType">
              <j-dict-select-tag
                type="list"
                v-model="model.materialType"
                dictCode="hf_material_type,material_type_name,id"
                placeholder="请选择物料类型"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料计量单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-select v-model="model.unit" @select="changeValue">
                <a-select-option v-for="(item, index) in dictOptions" :key="index" :value="item.id">
                  {{ item.code }}--{{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="默认仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouse">
              <j-dict-select-tag
                type="list"
                v-model="model.warehouse"
                dictCode="hf_warehouse,warehouse_name,id"
                placeholder="请选择物料默认仓库"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单箱数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perNumber">
              <a-input type="list" v-model="model.perNumber" placeholder="请输入单箱数" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="数量显示精度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accuracy">
              <a-input v-model="model.accuracy" placeholder="请输入数量显示精度"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="最大库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maxStorage">
              <a-input-number
                v-model="model.maxStorage"
                :min="1"
                placeholder="请输入最大库存"
                style="width: 100%"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="安全库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="safeStorage">
              <a-input-number
                v-model="model.safeStorage"
                :max="safeStorageMaxNum"
                placeholder="请输入安全库存"
                style="width: 100%"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="最小库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="minStorage">
              <a-input-number
                v-model="model.minStorage"
                :min="0"
                placeholder="请输入最小库存"
                style="width: 100%"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
import { cloneDeep } from 'lodash'

export default {
  name: 'HfMaterialForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      selectValue: '',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        materialName: [{ required: true, message: '请输入物料名称!' }],
        materialType: [{ required: true, message: '请输入物料类型!' }],
        unit: [{ required: true, message: '请输入物料计量单位!' }],
        warehouse: [{ required: true, message: '请输入仓库!' }],
        safeStorage: [
          {
            required: false,
            message: '安全库存不能大于最大库存!',
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (value > this.model.maxStorage) {
                this.$refs.form.validateField('safeStorage')
                callback('安全库存不能大于最大库存')
              } else {
                callback()
              }
            }
          }
        ]
      },
      url: {
        add: '/material/hfMaterial/add',
        edit: '/material/hfMaterial/edit',
        queryById: '/material/hfMaterial/queryById',
        unitList: '/material/hfMaterialUnit/noPagingList'
      },
      dictOptions: [],
      originalMaterialName: ''
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    safeStorageMaxNum() {
      return this.model.maxStorage
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  mounted() {
    this.getUnitList()
  },
  methods: {
    getUnitList() {
      getAction(this.url.unitList).then(res => {
        if (res.success) {
          let dict = res.result.records
          this.dictOptions = dict.map(item => {
            return {
              id: item.id,
              code: item.materialUnitCode,
              name: item.materialUnitName
            }
          })
        }
      })
    },
    add() {
      this.edit(this.modelDefault)
    },
    copyAdd(record) {
      this.originalMaterialName = record.materialName
      let temp = cloneDeep(record)
      this.edit({
        materialCode: temp.materialCode,
        materialName: temp.materialName,
        sap: temp.sap,
        materialType: temp.materialType,
        unit: temp.unit,
        warehouse: temp.warehouse,
        perNumber: temp.perNumber,
        maxStorage: temp.maxStorage,
        safeStorage: temp.safeStorage,
        minStorage: temp.minStorage,
        remark: temp.remark
      })
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      if (this.model.materialName === this.originalMaterialName) {
        this.$message.error('物料名称不能与原名称相同')
        return
      }

      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.model.materialCode === '') {
            this.model.materialCode = null
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    changeValue(id) {
      this.model.unit = id
    }
  }
}
</script>
